package com.example.toolproject.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.service.UserScenicSportService;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName UserScenicSportController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-06-28 10:38
 **/
@Controller
@CrossOrigin
@RequestMapping("/userScenicSport")
public class UserScenicSportController {

    @Autowired
    private UserScenicSportService userScenicSportService;

    /**
     * 执行用户选择景点操作
     * @param request
     * @param data  景点id
     * @return
     */
    @PostMapping("/insertUserScenicSport")
    @ResponseBody
    public ResultBean insertUserScenicSport(HttpServletRequest request, @RequestBody String data){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        /**
         * 获取用户id
         */
        int userId=u.getId();
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer scenicSportId1=jsonObject.getInteger("scenicSportId");
        /**
         * 判断用户选择的景点的个数
         */
        int count=userScenicSportService.getChoiceCount(userId);
        if(count>=3){
            return ResultBean.error("注意，只能选择三个景点",count);
        }
        //执行插入操作
        boolean flag=userScenicSportService.insertUserScenicSport(scenicSportId1,userId);
        if(flag==true){
            return ResultBean.success("选择景点成功",count+1);
        }else{
            return ResultBean.success("选择景点失败",count);
        }
    }


    /**
     * 执行用户取消选择景点操作
     * @param request
     * @param data  景点id
     * @return
     */
    @PostMapping("/deleteUserScenicSport")
    @ResponseBody
    public ResultBean deleteUserScenicSport(HttpServletRequest request, @RequestBody String data){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        /**
         * 获取用户id
         */
        int userId=u.getId();
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer scenicSportId1=jsonObject.getInteger("scenicSportId");
        /**
         * 查询当前用户已经选择了多少景点
         */
        int count=userScenicSportService.getChoiceCount(userId);
        //执行插入操作
        boolean flag=userScenicSportService.deleteUserScenicSport(scenicSportId1,userId);
        if(flag==true){
            return ResultBean.success("取消选择景点成功",count-1);
        }else{
            return ResultBean.success("取消选择景点失败",count);
        }
    }
}
